﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace BYManageInfoSystem
{
    public partial class Frm_RecvMoneyPayTaxAuxiliary_New : DockContent
    {
        private Dictionary<int, Color> dicSubSeqNo2Color = new Dictionary<int, Color>();
        private DGBY.BLL.FinRecvMoneyAuxiliaryTab_New m_bllFinRecvMoneyAuxiliaryTab_New = new DGBY.BLL.FinRecvMoneyAuxiliaryTab_New();
        private bool m_bSumStart = false;
        public Frm_RecvMoneyPayTaxAuxiliary_New()
        {
            InitializeComponent();
        }
        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllFinRecvMoneyAuxiliaryTab_New.GetFinRecvMoneyAuxiliaryInfo("FinRecvMoneyDetailTab_New.isNotJoinBackTax = 0");
            ultraGrid1.DataSource = null;
            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;


        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            
            ShowAllRecord("");
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            SummarySettings averageSummary =
e.Layout.Bands[0].Summaries.Add(
"退税值",
SummaryType.Sum,
e.Layout.Bands[0].Columns["退税值"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;

            averageSummary =
e.Layout.Bands[0].Summaries.Add(
"已退税值",
SummaryType.Sum,
e.Layout.Bands[0].Columns["已退税值"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;
            for (int i = 0; i < this.ultraGrid1.Rows.Count; i++)
            {
                UltraGridRow CurrentUGR = ultraGrid1.Rows[i];
                UltraGridRow PrevUGR = ultraGrid1.Rows[i > 0 ? i-1 : 0];
                //UltraGridRow NextUGR = ultraGrid1.Rows[(i < this.ultraGrid1.Rows.Count - 1) ? i + 1 : i];
                int nSubSeqNo = Convert.ToInt32(CurrentUGR.Cells["子序号"].Value.ToString(), 10);
                if (nSubSeqNo > 0)
                {
                    CurrentUGR.Cells["退税值"].Appearance.BackColor = dicSubSeqNo2Color[nSubSeqNo];
                    PrevUGR.Cells["已退税值"].Appearance.BackColor = dicSubSeqNo2Color[nSubSeqNo];
                }
            }

            InitColumnWidth();
        }

        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["子序号"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["客户"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["退税值"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["已退税值"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否已退税"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["退税日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["退税编号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
        }

        private void Frm_RecvMoneyPayTaxAuxiliary_New_Load(object sender, EventArgs e)
        {
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
            dicSubSeqNo2Color.Add(0, Color.Aqua);
            dicSubSeqNo2Color.Add(1, Color.Aquamarine);
            dicSubSeqNo2Color.Add(2, Color.Brown);
            dicSubSeqNo2Color.Add(3, Color.Beige);
            dicSubSeqNo2Color.Add(4, Color.Black);
            dicSubSeqNo2Color.Add(5, Color.BlanchedAlmond);
            dicSubSeqNo2Color.Add(6, Color.BlueViolet);
            dicSubSeqNo2Color.Add(7, Color.Azure);
            dicSubSeqNo2Color.Add(8, Color.Blue);
        }

        private void buttonSumStart_Click(object sender, EventArgs e)
        {
            m_bSumStart = true;
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            for (int i = 0; i < ugr.Length; i++)
            {
                for (int j = 0; j < ugr[i].Cells.Count; j++)
                {
                    if (ugr[i].Cells[j].Appearance.BackColor == Color.Aqua)
                    {
                        ugr[i].Cells[j].Appearance.BackColor = Color.FromName("0");
                    }
                }
            }
        }

        private void buttonSumStop_Click(object sender, EventArgs e)
        {
            try
            {
                Decimal dtTotal = 0;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    for (int j = 0; j < ugr[i].Cells.Count; j++)
                    {
                        if (ugr[i].Cells[j].Appearance.BackColor == Color.Aqua)
                        {
                            Decimal dtTemp = Convert.ToDecimal(ugr[i].Cells[j].Value.ToString());
                            dtTotal += dtTemp;
                            //ugr[i].Cells[j].Appearance.BackColor = Color.FromName("0");
                        }
                    }
                }

                this.textBoxTotal.Text = dtTotal.ToString();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void buttonExportExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void ultraGrid1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                UltraGrid grid = (UltraGrid)sender;
                UIElement element = grid.DisplayLayout.UIElement.LastElementEntered;

                Infragistics.Win.UltraWinGrid.ColumnHeader columnHeader = element.GetContext(typeof(Infragistics.Win.UltraWinGrid.ColumnHeader)) as Infragistics.Win.UltraWinGrid.ColumnHeader;
                if (columnHeader != null)
                {
                    string str = columnHeader.Caption;

                    Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                    if (ugr.Length <= 0)
                    {
                        return;
                    }
                    Decimal dtTemp = 0;
                    for (int i = 0; i < ugr.Length; i++)
                    {
                        string strTemp = ugr[i].Cells[str].Value.ToString();
                        if (strTemp != "")
                            dtTemp += Convert.ToDecimal(ugr[i].Cells[str].Value.ToString());
                    }
                    this.textBoxTotal.Text = dtTemp.ToString();




                }
                else
                {
                    // Set the grid's tooltip to null. 
                    //  Debug.WriteLine("null");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            if (m_bSumStart)
            {
                if (this.ultraGrid1.ActiveCell.Appearance.BackColor.Name == "0")
                {
                    this.ultraGrid1.ActiveCell.Appearance.BackColor = Color.Aqua;
                }
                else
                {
                    this.ultraGrid1.ActiveCell.Appearance.BackColor = Color.FromName("0");
                }
            }
        }

        private void textBoxTotal_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
